package com.cfw.util;

import org.apache.commons.lang3.ArrayUtils;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class StringUtil {

	private StringUtil() {}
	
	public static String enc(final String src, final String srcSet, final String tgtSet) {
		String buf = "";
		
		try {
			if (src == null) {
				buf = "";
			} else {
				buf = new String(src.getBytes(srcSet), tgtSet);
			}
		} catch (Exception e) {
			buf =	"";
		}
		return buf;
	}	

	public static String toString(Node node) {
		StringBuilder builder = new StringBuilder();
		
		if (node == null) { return builder.toString(); }

		if (node.getChildNodes().getLength() > 0) {
			builder.append(node.getNodeName()).append(":").append(XmlUtil.getNotEmptyText(node));
		} else {
			builder.append(node.getNodeName()).append(":").append(node.getNodeValue());
		}
		
		return builder.toString();
	}
	
	public static String toString(NodeList list) {
		StringBuilder builder = new StringBuilder();
		
		if (list == null) { return builder.toString(); }
		
		for (int i = 0; i < list.getLength(); i++) {
			if (i >= 0) { builder.append(","); }
			builder.append(toString(list.item(i)));
		}

		return builder.toString();
	}
	
	public static boolean hasMatchOne(final String[] source, final String[] target) {
		for (int i = 0; source != null && target != null && i < target.length; i++) {
			if (isMatch(source, target[i])) { return true; }
		}
		
		return false;
	}
		
	/**
	 * target정규식에 패턴 매칭되는 source 문자열이 array에 포함되어 있는지 여부를 반환.
	 * NOTE: target이 null일 경우 false 반환	 
	 * @param source
	 * @param target
	 * @return
	 */
	public static boolean isMatch(final String[] source, final String target) {
		for (int i = 0; target != null && source != null && i < source.length; i++) {
			if (source[i].matches(target)) { return true; }
		}
		
		return false;
	}	
	
	
}
